package com.naiterui.ehp.bs.recommend.service.impl;

import com.naiterui.ehp.bs.recommend.bean.RecomAuditOperationLog;
import com.naiterui.ehp.bs.recommend.repository.mysql.IRecomAuditOperationLogRepository;
import com.naiterui.ehp.bs.recommend.service.IRecomAuditOperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author guoyongxiang
 * @date 2019/12/9 15:40
 * @since 1.0.0
 */
@Service
public class RecomAuditOperationLogServiceImpl implements IRecomAuditOperationLogService {

    private String REMARK_TYPE_SEND = "医生推送处方";
    private String REMARK_TYPE_DISTRIBUTION = "分配处方给%s药师";
    private String REMARK_TYPE_REDISTRIBUTION = "撤回处方";
    private String REMARK_TYPE_PASS = "药师审核通过";
    private String REMARK_TYPE_NO_PASS = "药师审核失败，原因：%s";
    private String REMARK_TYPE_ALLOCATE = "调配药师审核通过";

    @Autowired
    private IRecomAuditOperationLogRepository recomAuditOperationLogRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLog(Long recomId, String operator, Integer type, String... args) {
        RecomAuditOperationLog log = new RecomAuditOperationLog();
        log.setRecomId(recomId);
        log.setRemark(this.getRemark(type, args));
        log.setType(type);
        log.setCreatedAt(new Date());
        log.setCreatedBy(operator);
        recomAuditOperationLogRepository.save(log);
    }

    @Override
    public String getRemark(Integer type, String... args) {
        String remark = "";
        switch (type) {
            case RecomAuditOperationLog.TYPE_SEND:
                remark = REMARK_TYPE_SEND;
                break;

            case RecomAuditOperationLog.TYPE_DISTRIBUTION:
                remark = String.format(REMARK_TYPE_DISTRIBUTION, args[0]);
                break;

            case RecomAuditOperationLog.TYPE_REDISTRIBUTION:
                remark = REMARK_TYPE_REDISTRIBUTION;
                break;

            case RecomAuditOperationLog.TYPE_PASS:
                remark = REMARK_TYPE_PASS;
                break;

            case RecomAuditOperationLog.TYPE_NO_PASS:
                remark = String.format(REMARK_TYPE_NO_PASS, args[0]);
                break;

            case RecomAuditOperationLog.TYPE_ALLOCATE:
                remark = REMARK_TYPE_ALLOCATE;
                break;

            default:

        }
        return remark;
    }
}
